#include "ButtonLayer.h"
#include "esp_log.h"

static const char *TAG = "GPIO BUTTON TEST";
static const char *TAG_2 = "GPIO  BUTTON 2 TEST";

static const char *TAG_KEY_R = "KEY_R";
static const char *TAG_KEY_L = "KEY_L";
static const char *TAG_KEY_R_LONG = "KEY_R_LONG";
static const char *TAG_KEY_L_LONG = "KEY_L_LONG";





void debug_button(void)
{
    ESP_LOGI(TAG, "button init");
}
// 定义四个按键虚拟对象回调函数
button_f buttons[KEY_COUNT] = {&debug_button, &debug_button, &debug_button, &debug_button};    // 定义四个按键回调函数作为指针数组

static void button_event_cb(void *arg, void *data)
{
    button_event_t event = iot_button_get_event(arg);     // 获取事件类型
    ESP_LOGI(TAG, "%s", iot_button_get_event_str(event)); // 打印事件类型

    if (BUTTON_PRESS_REPEAT == event || BUTTON_PRESS_REPEAT_DONE == event)
    {
        ESP_LOGI(TAG, "\tREPEAT[%d]", iot_button_get_repeat(arg));
    }

    if (BUTTON_PRESS_UP == event || BUTTON_LONG_PRESS_HOLD == event || BUTTON_LONG_PRESS_UP == event)
    {
        ESP_LOGI(TAG, "\tTICKS[%" PRIu32 "]", iot_button_get_ticks_time(arg));
    }

    if (BUTTON_MULTIPLE_CLICK == event)
    {
        ESP_LOGI(TAG, "\tMULTIPLE[%d]", (int)data);
    }
}

static void button_2_event_cb(void *arg, void *data)
{
    button_event_t event = iot_button_get_event(arg);     // 获取事件类型
    ESP_LOGI(TAG_2, "%s", iot_button_get_event_str(event)); // 打印事件类型

    if (BUTTON_PRESS_REPEAT == event || BUTTON_PRESS_REPEAT_DONE == event)
    {
        ESP_LOGI(TAG_2, "\tREPEAT[%d]", iot_button_get_repeat(arg));
    }

    if (BUTTON_PRESS_UP == event || BUTTON_LONG_PRESS_HOLD == event || BUTTON_LONG_PRESS_UP == event)
    {
        ESP_LOGI(TAG_2, "\tTICKS[%" PRIu32 "]", iot_button_get_ticks_time(arg));
    }

    if (BUTTON_MULTIPLE_CLICK == event)
    {
        ESP_LOGI(TAG_2, "\tMULTIPLE[%d]", (int)data);
    }
}

static void button_R_press_up_cb(void *arg, void *data)
{
    button_event_t event = iot_button_get_event(arg);     // 获取事件类型
    ESP_LOGI(TAG_KEY_R, "%s", iot_button_get_event_str(event)); // 打印事件类型

    if (BUTTON_PRESS_UP == event)
    {
        ESP_LOGI(TAG_KEY_R, "\tTICKS[%" PRIu32 "]", iot_button_get_ticks_time(arg));
        buttons[KEY_R]();
    }
}

static void button_L_press_up_cb(void *arg, void *data)
{
    button_event_t event = iot_button_get_event(arg);     // 获取事件类型
    ESP_LOGI(TAG_KEY_L, "%s", iot_button_get_event_str(event)); // 打印事件类型

    if (BUTTON_PRESS_UP == event)
    {
        ESP_LOGI(TAG_KEY_L, "\tTICKS[%" PRIu32 "]", iot_button_get_ticks_time(arg));
        buttons[KEY_L]();
    }
}

static void button_R_long_cb(void *arg, void *data)
{
    button_event_t event = iot_button_get_event(arg);     // 获取事件类型
    ESP_LOGI(TAG_KEY_R_LONG, "%s", iot_button_get_event_str(event)); // 打印事件类型

    // if (BUTTON_PRESS_UP == event)
    {
        ESP_LOGI(TAG_KEY_R_LONG, "\tTICKS[%" PRIu32 "]", iot_button_get_ticks_time(arg));
        buttons[KEY_R_LONG]();
    }
}

static void button_L_long_cb(void *arg, void *data)
{
    button_event_t event = iot_button_get_event(arg);     // 获取事件类型
    ESP_LOGI(TAG_KEY_L_LONG, "%s", iot_button_get_event_str(event)); // 打印事件类型

    // if (BUTTON_PRESS_UP == event)
    {
        ESP_LOGI(TAG_KEY_L_LONG, "\tTICKS[%" PRIu32 "]", iot_button_get_ticks_time(arg));
        buttons[KEY_L_LONG]();
    }	
}





button_f buttons_setFunc(btn_t btn, button_f func)
{
    if(btn >= KEY_COUNT || btn < 0)
    {
        ESP_LOGE(TAG, "Invalid button");
        return NULL;    
    }

    button_f old = buttons[btn];
	buttons[btn] = func;
	return old;
}

void buttons_setFuncs(button_f func1, button_f func2, button_f func3, button_f func4)
{
	buttons[KEY_R] = func1;
	buttons[KEY_L] = func2;
	buttons[KEY_R_LONG] = func3;
	buttons[KEY_L_LONG] = func4;
}



void xm_button_2_init(void)
{
    const button_config_t btn_cfg = {0};
    const button_gpio_config_t btn_gpio_cfg = {
       .gpio_num = BUTTON_2_IO_NUM,
       .active_level = BUTTON_ACTIVE_LEVEL,
       .enable_power_save = true,
    };

    button_handle_t btn = NULL;
    esp_err_t ret =
    iot_button_new_gpio_device(&btn_cfg, &btn_gpio_cfg, &btn);

    // iot_button_register_cb(btn, BUTTON_PRESS_DOWN, NULL, button_2_event_cb, NULL);
    iot_button_register_cb(btn, BUTTON_PRESS_UP, NULL, button_R_press_up_cb, NULL);
    // iot_button_register_cb(btn, BUTTON_PRESS_REPEAT, NULL, button_2_event_cb, NULL);
    // iot_button_register_cb(btn, BUTTON_PRESS_REPEAT_DONE, NULL, button_2_event_cb, NULL);
    // iot_button_register_cb(btn, BUTTON_SINGLE_CLICK, NULL, button_2_event_cb, NULL);
    // iot_button_register_cb(btn, BUTTON_DOUBLE_CLICK, NULL, button_2_event_cb, NULL);

    /*!< Multiple Click must provide button_event_args_t */
    /*!< Double Click */
    button_event_args_t args = {
        .multiple_clicks.clicks = 2,
    };
    iot_button_register_cb(btn, BUTTON_MULTIPLE_CLICK, &args, button_2_event_cb, (void *)2);
    /*!< Triple Click */
    args.multiple_clicks.clicks = 3;
    // iot_button_register_cb(btn, BUTTON_MULTIPLE_CLICK, &args, button_2_event_cb, (void *)3);
    iot_button_register_cb(btn, BUTTON_LONG_PRESS_UP, NULL, button_R_long_cb, NULL);
    // iot_button_register_cb(btn, BUTTON_LONG_PRESS_HOLD, NULL, button_2_event_cb, NULL);
    // iot_button_register_cb(btn, BUTTON_LONG_PRESS_UP, NULL, button_2_event_cb, NULL);
    // iot_button_register_cb(btn, BUTTON_PRESS_END, NULL, button_2_event_cb, NULL);
}

void xm_button_init(void)
{
    const button_config_t btn_cfg = {0};
    const button_gpio_config_t btn_gpio_cfg = {
        .gpio_num = BUTTON_IO_NUM,
        .active_level = BUTTON_ACTIVE_LEVEL,
        .enable_power_save = true,
    };

    button_handle_t btn = NULL;
    esp_err_t ret =
    iot_button_new_gpio_device(&btn_cfg, &btn_gpio_cfg, &btn);

    // iot_button_register_cb(btn, BUTTON_PRESS_DOWN, NULL, button_event_cb, NULL);
    iot_button_register_cb(btn, BUTTON_PRESS_UP, NULL, button_L_press_up_cb, NULL);
    // iot_button_register_cb(btn, BUTTON_PRESS_REPEAT, NULL, button_event_cb, NULL);
    // iot_button_register_cb(btn, BUTTON_PRESS_REPEAT_DONE, NULL, button_event_cb, NULL);
    // iot_button_register_cb(btn, BUTTON_SINGLE_CLICK, NULL, button_event_cb, NULL);
    // iot_button_register_cb(btn, BUTTON_DOUBLE_CLICK, NULL, button_event_cb, NULL);

    /*!< Multiple Click must provide button_event_args_t */
    /*!< Double Click */
    button_event_args_t args = {
        .multiple_clicks.clicks = 2,
    };
    // iot_button_register_cb(btn, BUTTON_MULTIPLE_CLICK, &args, button_event_cb, (void *)2);
    /*!< Triple Click */
    args.multiple_clicks.clicks = 3;
    iot_button_register_cb(btn, BUTTON_MULTIPLE_CLICK, &args, button_event_cb, (void *)3);
    // iot_button_register_cb(btn, BUTTON_LONG_PRESS_START, NULL, button_event_cb, NULL);
    // iot_button_register_cb(btn, BUTTON_LONG_PRESS_HOLD, NULL, button_event_cb, NULL);
    /*!< Multiple Click must provide button_event_args_t */
    /*!< Long Press */
    args.long_press.press_time = 2000;
    iot_button_register_cb(btn, BUTTON_LONG_PRESS_UP, &args, button_L_long_cb, NULL);
    // iot_button_register_cb(btn, BUTTON_PRESS_END, NULL, button_event_cb, NULL);
}
